<?php
	//include_once('protocol.php');
	define('UI_TIMEOUT',3);
	include_once( dirname(dirname(dirname(__FILE__))) . '/core/public/library/common/network/fiveonelib_sock_stream.php');
	class UIReqHead
	{
		var $total_len_;
		var $msg_id_;
		var $pad_;
		var $zone_id_;
		var $role_id_;
		var $body_len_;

		function __construct()
		{
			$this->total_len_ = 0;
			$this->msg_id = 0;
			$this->pad_ = 0;
			$this->zone_id_ = 0;
			$this->role_id_ = 0;
			$this->body_len_ = 0;
		}
		function encode()
		{
			$this->total_len_ = $this->body_len_ + 16;
			return pack("NnnNN", $this->total_len_,
				$this->msg_id_,
				$this->pad_,
				$this->zone_id_,
				$this->role_id_);
		}
	}

	class UIRespHead
	{

		var $total_len_;
		var $error_code_;
		var $msg_id_;
		var $pad_;
		var $zone_id_;
		var $role_id_;
		var $body_len_;
		var $raw_body_;

		function __construct()
		{
			$total_len_ = 0;
			$error_code_ = 0;
			$msg_id_ = 0;
			$pad_ = 0;
			$zone_id_ = 0;
			$role_id_ = 0;
		}
		
		function decode($data)
		{
			$offset_total_len = 0;
			$offset_error_code = 4;
			$offset_msg_id = 8;
			$offset_pad = 10;
			$offset_zone_id = 12;
			$offset_role_id = 16;
			$offset_body = 20;

			$res = unpack("Ntl/Nec/nmi/npd/Nzi/Nri", $data);
			$this->total_len_ = $res['tl'];
			$this->error_code_ = $res['ec'];
			$this->msg_id_ = $res['mi'];
			$this->pad_ = $res['pd'];
			$this->zone_id_ = $res['zi'];
			$this->role_id_ = $res['ri'];
			$this->body_len_ = $this->total_len_ - 20;
			$this->raw_body_ = substr($data, 20, $this->body_len_);
		}
	}


	class DMZ_Socket
	{
		var $sock;
		var $connected;
		function __construct()
		{
			$this->sock = -1;
			$this->connected = false;
		}
		
		function connectServer($host, $port, $bool = true)
		{
		    $socket = new Fiveonelib_SockStream(AF_INET, SOL_TCP);
		    $socket->connect($host, $port, UI_TIMEOUT);
		    if ($bool !== true && !$socket->isConnected())
		    {
		        return false;
		    }
		    if (!$socket->isConnected())
		    {
		        //echo json_encode(array('error'=>'服务器连接超时！', 'message'=>'服务器连接超时！'));
		       // exit;
		    }
		    return $socket;
		}
		
		function open_connect($ip, $port)
		{
			$this->sock = $this->connectServer($ip, $port);
			if ($this->sock ) {
				$this->connected = true;
			}
		}
	}

	class DMZ_UIClient
	{
		var $socket;

		function __construct()
		{
			$this->socket = new DMZ_Socket();
		}

		function  get_ui_host()
		{
			//10.10.5.157
			$hostarr = array(0=>array('192.168.9.139',44001),1=>array('192.168.9.139',44001));
			return $hostarr;
		}

		function get_role_info($zone_id, $role_id)
		{
			$req = new UIReqHead();
			$req->msg_id_ = 1;
			$req->zone_id_ = $zone_id;
			$req->role_id_ = $role_id;

			$to_send = $req->encode();
			$hostarr = $this->get_ui_host();
			$rand = rand(0,1);
			$this->socket->open_connect($hostarr[$rand][0],$hostarr[$rand][1]);// host port
			$ret = $this->socket->sock->write($to_send,strlen($to_send),UI_TIMEOUT, 5);
			
			if (FALSE === $ret)
			{
				return false;
			}
			$resp_raw = $this->socket->sock->read(8192, UI_TIMEOUT);
			if (!$resp_raw)
				return false;
			$this->socket->sock->disconnect();
			$resp = new UIRespHead();
			$resp->decode($resp_raw);

			$role_info = json_decode($resp->raw_body_,true);
			if ($role_info)
				return $role_info;
			
			return false;	
		}

		function get_rank_info($zone_id)
		{
			$req = new UIReqHead();
			$req->msg_id_ = 2;
			$req->zone_id_ = $zone_id;
			$req->role_id_ = 142729555;		
		
			$to_send = $req->encode();
			$hostarr = $this->get_ui_host();
			$rand = rand(0,1);
			$this->socket->open_connect($hostarr[$rand][0],$hostarr[$rand][1]);// host port
			$ret = $this->socket->sock->write($to_send,strlen($to_send), UI_TIMEOUT, 5);
			if (FALSE == $ret)
			{
				return false;
			}
			$resp_raw = $this->socket->sock->read(8192, UI_TIMEOUT, 5);
			if (!$resp_raw)
				return false;
			$this->socket->sock->disconnect();
			$resp = new UIRespHead();
			$resp->decode($resp_raw);

			$role_info = json_decode($resp->raw_body_,true);
			if ($role_info)
				return $role_info;
			
			return false;	
		}

	}
?>
	

	

